package com.example.myapplication.module1.fragment

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView.VERTICAL
import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentCustomerManagerBinding
import com.example.myapplication.entity.ItemBean
import com.example.myapplication.module1.ItemGridSpanSizeLookup
import com.example.myapplication.module1.adapter.CategoryContentAdapter
import com.example.mylibrary.base.BaseFragment
import com.example.mylibrary.utils.UiTools

/**
 * 首页分类fragment,可不用,目前使用recyclerview 实现了
 */

@Deprecated("class")
class CustomerManagerFragment : BaseFragment<FragmentCustomerManagerBinding>() {
    private val mutableList = mutableListOf<Any>()
    private val itemBeanList = mutableListOf<ItemBean>()

    private lateinit var customerManagerTitle: Array<String>
    private lateinit var secretary: Array<String>
    private lateinit var workPlatform: Array<String>
    private lateinit var installRepair: Array<String>
    private lateinit var planSummary: Array<String>

    private val categoryContentAdapter: CategoryContentAdapter? by lazy {
        activity?.let { CategoryContentAdapter(it) }
    }

    override fun getBinding(): FragmentCustomerManagerBinding {
        return FragmentCustomerManagerBinding.inflate(layoutInflater)
    }

    override fun initView(view: View) {
        val serviceNotice = UiTools.resources.obtainTypedArray(R.array.secretaryImage)
        val workPlatformArray = UiTools.resources.obtainTypedArray(R.array.workPlatformImage)
        val installFixed = UiTools.resources.obtainTypedArray(R.array.installRepairImage)
        val plan = UiTools.resources.obtainTypedArray(R.array.planSummaryImage)

        mutableList.clear()
        customerManagerTitle = UiTools.getStringArray(R.array.customerManagerTitle)
        secretary = UiTools.getStringArray(R.array.secretary)
        workPlatform = UiTools.getStringArray(R.array.workPlatform)
        installRepair = UiTools.getStringArray(R.array.installRepair)
        planSummary = UiTools.getStringArray(R.array.planSummary)

        val gridLayoutManager = GridLayoutManager(activity, 4, VERTICAL, false)

        for (i in customerManagerTitle.indices) {
            itemBeanList.clear()
            mutableList.add(customerManagerTitle[i])
            when (i) {
                0 -> {
                    for (index in secretary.indices) {
                        itemBeanList.add(
                            ItemBean(
                                1,
                                i,
                                index,
                                "1$i$index",
                                secretary[index],
                                serviceNotice.getResourceId(index, 0)
                            )
                        )
                    }
                }
                1 -> {
                    for (index in workPlatform.indices) {
                        itemBeanList.add(
                            ItemBean(
                                1,
                                i,
                                index,
                                "1$i$index",
                                workPlatform[index],
                                workPlatformArray.getResourceId(index, 0)
                            )
                        )
                    }
                }
                2 -> {
                    for (index in installRepair.indices) {
                        itemBeanList.add(
                            ItemBean(
                                1,
                                i,
                                index,
                                "1$i$index",
                                installRepair[index],
                                installFixed.getResourceId(index, 0)
                            )
                        )
                    }
                }
                3 -> {
                    for (index in planSummary.indices) {
                        itemBeanList.add(
                            ItemBean(
                                1,
                                i,
                                index,
                                "1$i$index",
                                planSummary[index],
                                plan.getResourceId(index, 0)
                            )
                        )
                    }
                }
            }
            mutableList.addAll(itemBeanList)
        }
        serviceNotice.recycle()
        workPlatformArray.recycle()
        installFixed.recycle()
        plan.recycle()
        viewBinding.rvList.layoutManager = gridLayoutManager
        gridLayoutManager.spanSizeLookup = ItemGridSpanSizeLookup(mutableList, 4)
        viewBinding.rvList.adapter = categoryContentAdapter

        categoryContentAdapter?.mutableList = mutableList
    }


}
